package com.CityHunter.Adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.CityHunter.R;
import com.CityHunter.DataObject.Buddy;
import com.CityHunter.DataObject.Group;

public class BuddyAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private Bitmap bdyOnlineIcon, bdyOfflineIcon, groupIcon;
	private List<Buddy> buddies;
	private List<Group> groups;

	public BuddyAdapter() {

	}

	public BuddyAdapter(Context context, List<Buddy> buddies) {

		mInflater = LayoutInflater.from(context);
		this.buddies = buddies;
		this.groups = new ArrayList<Group>();

		bdyOnlineIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.online);
		bdyOfflineIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.offline);
		groupIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.group);
	}

	public BuddyAdapter(Context context, List<Buddy> buddies, List<Group> groups) {

		mInflater = LayoutInflater.from(context);
		this.buddies = buddies;
		this.groups = groups;

		bdyOnlineIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.online);
		bdyOfflineIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.offline);
		groupIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.group);
	}

	@Override
	public int getCount() {
		return buddies.size() + groups.size();
	}

	@Override
	public Object getItem(int position) {
		if (position < buddies.size())
			return ((Buddy) buddies.get(position)).getName();
		else
			return ((Group) groups.get(position)).getTitle();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			// Use customize List Row Layout
			convertView = mInflater.inflate(R.layout.bdyr, null);
			holder = new ViewHolder();
			holder.text = (TextView) convertView
					.findViewById(R.id.bdyr_tv_name);
			holder.icon = (ImageView) convertView
					.findViewById(R.id.bdyr_iv_state);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (position < buddies.size()) {
			Buddy bb = (Buddy) buddies.get(position);

			if (bb.getState() == 0) {
				holder.text.setText(bb.getName());
				holder.icon.setImageBitmap(bdyOfflineIcon);
			} else {
				holder.text.setText(bb.getName());
				holder.icon.setImageBitmap(bdyOnlineIcon);
			}
		} else {
			Group gb = (Group) groups.get(position - buddies.size());
			holder.text.setText(gb.getTitle());
			holder.icon.setImageBitmap(groupIcon);
		}

		return convertView;
	}

	// Class ViewHolder
	private class ViewHolder {
		TextView text;
		ImageView icon;
	}

}
